package com.huiquan.mark_sentence.controller;

import com.huiquan.analysis.domain.User;
import com.huiquan.analysis.service.AnalysisSentenceService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.mark_sentence.service.MarkSentenceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 语句标记控制类
 * @Date 2018/4/24 09:15
 * @since Ver V0.0.1
 */
@Controller
@RequestMapping("/mark_sentence")
public class MarkSentenceController {

    @Autowired
    private MarkSentenceService markSentenceService;
    @Autowired
    private AnalysisSentenceService analysisSentenceService;

    @RequestMapping("/sentence_list.htm")
    public ModelAndView sentenceList(HttpServletRequest req) {
        String tabid = req.getParameter("tabid");
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            if (tabid.endsWith("1")) {
                pageParam.put("flag", 0);
                pageParam.put("delFlag", 0);
            } else if (tabid.endsWith("2")) {
                pageParam.put("flag", 1);
                pageParam.put("delFlag", 0);
            } else if (tabid.endsWith("3")) {
                pageParam.put("delFlag", 1);
            }
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }
        tabid = (String) pageParam.get("tabid");
        // 如果没有进行全部的未标注则重定向进入未标注界面
        if (StringUtils.isNotBlank(markSentenceService.getUnlabelId())) {
            String[] idAndType = markSentenceService.getUnlabelId().split((char) 1 + "");
            // 现病史
            pageParam.put("tabid", "12_0");
            if ("41_2".equals(tabid)) {
                pageParam.put("markSentenceFlag", "2");
            } else {
                pageParam.put("markSentenceFlag", "1");
            }
            return new ModelAndView("redirect:/analysis/sentence_pre_edit.htm?id=" + idAndType[0] + "&type=2&secondType=" + idAndType[1]);
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        String flag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "flag");
        String delFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "delFlag");

        String keyword = req.getParameter("keyword");
        return markSentenceService.getSentenceList(flag, keyword, start,delFlag);
    }

    @RequestMapping("/sentence_pre.htm")
    public ModelAndView preEdit(String id) {
        return markSentenceService.preEdit(id);
    }

    @ResponseBody
    @RequestMapping("/export_sentence.do")
    public ReturnData exportSentence() {
        markSentenceService.exportMarkedSentence();
        return ReturnUtil.success();
    }

    /**
     * 删除(恢复)分类的句子
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete_sentence.do")
    public ReturnData deleteSentence(@RequestParam(value = "id") Long id) {


        markSentenceService.deleteSentence(id);
        return ReturnUtil.success();
    }


    @ResponseBody
    @RequestMapping("/import_sentence.do")
    public ReturnData importSentence() {
        try {
            markSentenceService.importUnmarkSentence();
        } catch (Exception e) {
            throw new RuntimeException("初始化未标记句子失败");
        }
        return ReturnUtil.success();
    }

    /**
     * 进行词语编辑操作
     *
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/sentence_edit.do")
    public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        String idStr = req.getParameter("id");
        Map<String, Object> map = req.getParameterMap();
        String[] vocabularyStrs = (String[]) map.get("vocabulary");
        String[] characterStrs = (String[]) map.get("character");
        String[] propertyStrs = (String[]) map.get("property");
        String[] position1Strs = (String[]) map.get("position1");
        String[] position2Strs = (String[]) map.get("position2");
        String[] sourceStrs = (String[]) map.get("source");

        ReturnData result = markSentenceService.edit(idStr, user, vocabularyStrs, characterStrs,
                propertyStrs, position1Strs, position2Strs, req, sourceStrs);
        // 更新现病史的增量搜索引擎
        analysisSentenceService.asynchronousUpdateSphinxIndex("vocabulary" + 2 + "_delta");
        return result;
    }
}
